// ==UserScript==
// @name        New script 
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @grant       none
// @version     1.0
// @author      -
// @description 2022/1/17 上午9:20:59
// @require    https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.6.0.slim.min.js
// ==/UserScript==

 
(function() {
    /*获取当前页面的url，用以判断执行哪一部分脚本*/
    var url = window.location.href;
    /*如果含有未完成的课程，则自动进入课程中*/
    if(url.indexOf('study/myclass/index')!=-1){
        var unfinished = document.getElementsByClassName('class-list-box')[0].getElementsByClassName('unfinished');
        if(unfinished.length>0){
            /*进入课程*/
            unfinished[0].getElementsByClassName('title')[0].click();
        }
        /*如果课程中含有未完成视频，则自动进入该课程,否则返回班级*/
    }else if(url.indexOf('study/myclass/course')!=-1){
        var finish_no = document.getElementsByClassName('list-box')[0].getElementsByClassName('finish-no');
        if(finish_no.length>0){
            /*进入视频*/
            finish_no[1].getElementsByTagName('button')[0].click();
        }else{
            /*返回班级*/
            document.getElementsByClassName('text-header')[0].getElementsByTagName('a')[0].click();
        }
    }else{
        /*当前观看的课程*/
        var current;
        /*课程中的所有课程*/
        var list;
        /*观看状态：1表示当前视频未看完，0表示当前视频已看完(已完成课程重刷)*/
        var lessonstatus = 1;
        /*查看模式：0代表未看完的方式，1代表已看完重刷一次，默认1即可刷所有视频，0只能刷未看完的视频*/
        var watchmode = 0;
        /*开始统计视频总时长(5秒内可自行选择起始视频)*/
        setTimeout(function() {
            /*分*/
            var minute = -1;
            /*秒*/
            var second = 0;
            /*课程内所有视频*/
            list = document.getElementById("list_chapter").getElementsByClassName("section");
            /*时间累加*/
            for(var i = 0; i < list.length; i++) {
                //定位到当前视频
                if(list[i].className.indexOf("active")!=-1) {
                    current = i;
                    minute = 0;
                    console.log("当前ID：" + i)
                }
                if(minute >= 0){
                    minute += parseInt(list[i].getElementsByClassName("time")[0].innerHTML.split(":")[0]);
                    second += parseInt(list[i].getElementsByClassName("time")[0].innerHTML.split(":")[1]);
                }
            }
            minute += parseInt(second/60);
            var time = document.createElement('div');
            time.style = "position:fixed;;top:20px;left:30%;background-color: pink;";
            time.innerText = '视频总长'+minute+'分'+second%60+'秒，已开启自动播放，感谢使用攸泠脚本！';
            document.body.append(time);
        }, 5000);
      
//         var v_id = document.getElementsByClassName("cell flash section active")[0].getAttribute("data-idx");
      
//         var s_time = document.getElementsByClassName("cell flash section active")[0].getElementsByClassName("time")[0].innerText;
//         console.log("视频时长：" + s_time + "|||"+"id 是" + v_id);
 
        /*刷已完成课程*/
        /*因为是iframe标签，所以使用监听非常麻烦，这里采用计时的方式*/
        /*考虑到网络延时，多增加了10秒延迟*/
        function watchover(){
            lessonstatus = 0;
            var minute = parseInt(list[current].getElementsByClassName("time")[0].innerHTML.split(":")[0]);
            var second = parseInt(list[current].getElementsByClassName("time")[0].innerHTML.split(":")[1])+10;
            if(second>=60){
                minute ++;
                second -= 60;
            }
            document.getElementById('list_chapter').getElementsByClassName("active")[0].innerHTML += "-"+minute+":"+second;
            console.log(current);
            setTimeout(function() {
                current ++;
                list[current].click();
                lessonstatus = 1;
            }, (minute * 60 + second) * 1000);
        }
        /*刷未完成课程*/
        function watchnew(){
            var test = list[current].getElementsByClassName("status-done")[0].innerText;
            //判断是否播放完成
            if(current >= list.length-1){
                document.getElementsByClassName('btn-back')[0].getElementsByTagName('a')[0].click();
            }else{
                if(typeof(test) != "undefined") {
                    console.log("视频已播放完成:" + test);
                    current +=2;
                    list[current].click();
                }
            }
        }
        /*定时启动判断程序*/
        setInterval(function() {
            if (watchmode == 1){
                if (lessonstatus == 1){
                    watchover();
                }
            }else{
                watchnew();
            }
        },10000)
    }
})();


/*
console.log("开始运行");
(window.onload = function(){
  
  
  function click_btn(){
    var finish_btn = document.getElementById("btn_submit");
    finish_btn.click();
  }
 
  
  //点击下一个视频
  function click_next(count){
    s = count + 2;
    var next_work = document.querySelector("div[data-idx=" + s + "]");
    next_work.click();
  }
  var finish_btn = document.getElementById("btn_submit");

  console.log("按钮文字是：" + finish_btn.innerText);
  
  let work_list = document.getElementsByClassName("cell flash section");
  let index = 0;
  for(let i = 0; i<work_list.length; i++){ // 寻找当前元素
　　if("cell flash section active" == work_list.item(i).className) index = i
}
  console.log(work_list);
  

        
  //休息
  function sleep(times){
    var now = new Date();
    var exitTime = now.getTime() + times;
    
    while(true){
      now = new Date();
      if (now.getTime() > exitTime )
        return;
      
    }

  }      
  
  //检测时间 执行点击下一个视频
  function check_time(){
    var current_time = document.querySelector("#vidplayer_component_3A21A4F4-29EE-4FF3-AC60-6E44FF1B9F70 > span.current-time").innerText;
    var end_time = document.querySelector("#list_chapter > div.cell.flash.section.active > span.meta > span.time").innerText;
    console.log("当前视频时长： " + end_time);
    while(true){
      if (current_time===end_time) {
        console.log("视频已结束，点击下一个视频！");
        click_next(index);
      }
    
        sleep(5000);
    } 
  }
  
    //找到当前播放视频
  var active_cla = document.getElementsByClassName("cell flash section active");
  var active_count = active_cla[0];
  sleep(1000);
  
  console.log('当前运行的ele 是：' + active_count);
  //var active_count = active_cla.getAttribute("data-jhx-res");
 // console.log("当前视频值：" + active_count);
  //alert(active_cla.getAttribute("data-jhx-res")); 
  check_time();
  
})(); */